<?php 
	$patient_id = 0;

	//for form submit
	if($_SERVER['REQUEST_METHOD'] == "POST"){
		
		//form post arrays for patient_group
		$patient_group_post = array(
				'group_id' 		=> $_POST['group_id'],
				'patient_id' 	=> $_POST['patient_id']
			);

		//form post array for user_patient
		if(count($_POST['practicioner'])){
			$practicioner_post 	= array_filter($_POST['practicioner']);
			$user_patient_post 	= $practicioner_post;
		}
		if(count($_POST['non_practicioner'])){
			$nonpracticioner_post = array_filter($_POST['non_practicioner']);
			$user_patient_post = array_merge($user_patient_post, $nonpracticioner_post);
		}
		
		//form post array for patient
		$patient_post = $_POST;
		$patient_post['birthday'] 		= date("Y-m-d", strtotime($_POST['birthday']));
		$patient_post['referral_date'] 	= date("Y-m-d", strtotime($_POST['referral_date']));
		unset($patient_post['group_id']);
		unset($patient_post['user_id']);
		unset($patient_post['patient_id']);
		unset($patient_post['practicioner']);
		unset($patient_post['non_practicioner']);

		if(isset($_POST['id']) && !empty($_POST['id'])){
			$patient_id = $_POST['id'];

			//edit patient
			$patient_def = array(
							'update' => $patient_post,
							'filter' => 'id = '.$_POST['id']
						  );
			$patient = new Patient($patient_def);
			$patient->Update();

			//edit patient_group

			//edit user_patient
			if(count($user_patient_post) && !empty($patient_id)){
				$group_id = $_POST['group_id'];
				
				//delete all existing entries for this group first
				$obj =  new UserPatient();
				$obj->filter = "user_group_id = ".$group_id." AND patient_id = ".$patient_id;
				$obj->Delete();

				//insert to DB
				foreach($user_patient_post as $key => $val){
					unset($params);
					unset($insert_params);
					unset($patient_user);

					$params = array(
							'patient_id' 	=> $patient_id,
							'user_group_id' => $group_id,
							'user_id' 		=> $val
						);
					$insert_params = array('add' => $params);
					$patient_user = new UserPatient($insert_params);
					$patient_user->Insert();
				}
			}
			$message = 2;

		}else{
			//add patient
			$patient_post['date_added'] = date("Y-m-d h:i:s");  
			$patient_def 	= array('add' => $patient_post);
			$patient 		= new Patient($patient_def);
			$patient_id 	= $patient->Insert();

			//add to patient_group
			if(!empty($_POST['group_id'])){
				$patient_group_post['patient_id'] = $patient_id;
				$patient_group_def 	= array('add' => $patient_group_post);
				$patient_group 		= new PatientGroup($patient_group_def);
				$patient_group->Insert();
			}

			//add to user_patient
			if(count($user_patient_post) && !empty($patient_id)){
				$group_id = $_POST['group_id'];
				
				//delete all existing entries for this group first
				$obj =  new UserPatient();
				$obj->filter = "user_group_id = ".$group_id." AND patient_id = ".$patient_id;
				$obj->Delete();

				//insert to DB
				foreach($user_patient_post as $val){
					$params = array(
							'patient_id' 	=> $patient_id,
							'user_group_id' => $group_id,
							'user_id' 		=> $val
						);
					$insert_params = array('add' => $params);
					$patient_user  = new UserPatient($insert_params);
					$patient_user->Insert();
				}
			}
			$message = 1;
		}
	}

	//for edit form
	if($_SERVER['REQUEST_METHOD'] == "GET"){
		$message = 0;
		if(isset($_GET['id']) && !empty($_GET['id'])){
			$patient_id = $_GET['id'];

			//Note: user group seen here should be based on session set for this user - Change this
			//fetch patient and patient group details
			$params 	 = array('filter' => 'patient.id = '.$patient_id);
			$patient 	 = new Patient($params);
			$patient->AddJoin("LEFT JOIN", "patient_group", "patient_group.patient_id=".$patient_id, "patient_group.group_id");
			$patient_row = $patient->Select();

			//fetch all user belonging to the group
			$userparams = array(
						'select' => 'user_id',
						'filter' => 'user_group_id = '.$patient_row['group_id'].' AND patient_id ='.$patient_id
					);
			$user_patient = new UserPatient($userparams);
			$user_patient_result = $user_patient->SelectAll();
			if(count($user_patient_result)){
				foreach($user_patient_result as $val_id){
					$user_patient_row[] = $val_id['user_id'];
				}
			}
		}
	}

	//for select lists
	//dropdown select for atsi
	$atsiObj = new Atsi();
	if(!empty($patient_id) && isset($patient_row['atsi']) && !empty($patient_row['atsi'])){
		$atsiObj->select_list_default = $patient_row['atsi'];
	}
	$atsi_select = $atsiObj->SelectList();

	//dropdown select for health fund type
	$healthObj = new HealthFundType();
	if(!empty($patient_id) && isset($patient_row['health_fund_type']) && !empty($patient_row['health_fund_type'])){
		$healthObj->select_list_default = $patient_row['health_fund_type'];
	}
	$health_fund_type_select = $healthObj->SelectList();

	//dropdown select for health fund
	$healthFundObj = new HealthFund();
	if(!empty($patient_id) && isset($patient_row['health_fund_name']) && !empty($patient_row['health_fund_name'])){
		$healthFundObj->select_list_default = $patient_row['health_fund_name'];
	}
	$health_fund_select = $healthFundObj->SelectList();

	//dropdown select for group
	$groupObj =  new Group();
	if(!empty($patient_id) && isset($patient_row['group_id']) && !empty($patient_row['group_id'])){
		$groupObj->select_list_default = $patient_row['group_id'];
	}
	$group_select = $groupObj->SelectList();

	//dropdown select for practicioner
	$pracObj =  new UserPracticioner();
	if($_SESSION['user']['user_type_id'] != CONST_SUPER_ADMIN_TYPE && !empty($_SESSION['user']['group_id'])){
		$pracObj->AddJoin("LEFT JOIN", "user_group", "user_practicioner.user_id = user_group.user_id", "user_group.group_id");
		$pracObj->filter = "user_group.group_id = ".$_SESSION['user']['group_id'];
	}
	if(count($user_patient_row) && !empty($patient_id)){
		$pracObj->select_list_default = $user_patient_row;
	}
	$prac_select = $pracObj->SelectList();

	//dropdown select for non practicioner
	$nonpracObj =  new UserNonPracticioner();
	if($_SESSION['user']['user_type_id'] != CONST_SUPER_ADMIN_TYPE && !empty($_SESSION['user']['group_id'])){
		$pracObj->AddJoin("LEFT JOIN", "user_group", "user_non_practicioner.user_id = user_group.user_id", "user_group.group_id");
		$pracObj->filter = "user_group.group_id = ".$_SESSION['user']['group_id'];
	}
	if(count($user_patient_row) && !empty($patient_id)){
		$nonpracObj->select_list_default = $user_patient_row;
	}
	$non_prac_select = $nonpracObj->SelectList();
?>